package ilvonen.ihstatistics.repository;

import ilvonen.ihstatistics.models.ConstantData;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

public class DBLeagues {

		//define table name
		private static final String TABLE_NAME = "leagues";
	//define tables column names
		private static final String KEY_ID = "_id";
		private static final String KEY_NAME = "name";
	//define create statement for all your tables. Create new file (class) for each table.
		private static final String CREATE_STATEMENT = "create table if not exists " + TABLE_NAME + " (" + KEY_ID + " integer primary key autoincrement, " + KEY_NAME + " text not null);";
	
	/*
	 * CLASS STATUS: READY!
	 */
	public DBLeagues(){}
	/*
	 * 
	 */
	public String create(){
		return CREATE_STATEMENT;
	}
	/*
	 * 
	 */
	public long insert(String name, DBAdapter db){
		
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_NAME, name);
		
		return db.insertRecord(TABLE_NAME, initialValues);
		
	}
	/*
	 * 
	 */
	public boolean delete(long rowId, DBAdapter db){
		return db.deleteRecord(TABLE_NAME, KEY_ID, rowId);
	}
	/*
	 * 
	 */
	public List<ConstantData> getAllLeagueTypes(DBAdapter db){
		Cursor c;
		List<ConstantData> s = new ArrayList<ConstantData>();
		c = db.getAllRecords(TABLE_NAME, new String[]{KEY_ID,KEY_NAME});
		while(c.moveToNext()){
			ConstantData data = new ConstantData();
			data.set_id(c.getInt(0));
			data.set_name(c.getString(1));
	
			s.add(data);
		}
		return s;
	}
	/*
	 * 
	 */
	public ConstantData getLeagueTypeById(DBAdapter db, long ROW_ID){
		ConstantData s = null;
		Cursor c;
		c = db.getRecord(TABLE_NAME, new String[]{KEY_ID,KEY_NAME}, KEY_ID, ROW_ID);
		if(c != null){
			s = new ConstantData();
			c.moveToFirst();
			s.set_id(c.getInt(0));
			s.set_name(c.getString(1));
		}
		return s;
	}
}
